package com.dyypdadmin.controller;

import com.dyypdadmin.pojo.ServiceRecords;
import com.dyypdadmin.pojo.vo.ServiceRecordsDetails;
import com.dyypdadmin.result.Result;
import com.dyypdadmin.service.ServiceRecordsDetailsService;
import com.dyypdadmin.service.ServiceRecordsService;
import com.dyypdadmin.utils.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/serviceRecords")
public class ServiceRecordsController {
    @Autowired
    private ServiceRecordsService recordsService;
    @Autowired
    private ServiceRecordsDetailsService recordsDetailsService;
    /*
    * 添加服务记录
    * */
    @PostMapping("/addServiceRecord")
    public Result addRecord(@RequestBody Map<String,Object> serviceRecords){
        System.out.println(serviceRecords+"接收数据");
        Integer user_id = (Integer) serviceRecords.get("user_id"); // 用户id通过此字段可以查询所有记录
        String order_number = OrderNumberGenerator.generateOrderNumber(); // 订单号
        Integer pet_id = (Integer) serviceRecords.get("pet_id"); // 宠物id
        Integer appointment_id = (Integer) serviceRecords.get("appointment_id"); // 预约记录id
        Integer project_id = (Integer) serviceRecords.get("project_id"); // 项目名称
        Integer status = (Integer) serviceRecords.get("status"); // 订单状态：1-完成，2-院方拒绝，3-用户取消
        Integer is_medical = (Integer) serviceRecords.get("is_medical"); // 订单状态：1-完成，2-院方拒绝，3-用户取消
        System.out.println("user_id"+user_id);
        System.out.println("order_number"+order_number);
        System.out.println("pet_id"+pet_id);
        System.out.println("appointment_id"+appointment_id);
        System.out.println("project_id"+project_id);
        System.out.println("status"+status);
        int result = recordsService.addServiceRecord(user_id,order_number,pet_id,appointment_id,project_id,status,is_medical);
        if(result>0){
            return new Result(true,"新增成功");
        }else{
            return new Result(false,"新增失败");
        }
    }
    /*
    * 获取所有的服务记录
    *
    * */
    @GetMapping("/getAll")
    public Result getAll(){
        List<ServiceRecords> recordsList = recordsService.getAll();
        if(recordsList!=null){
            return new Result(true,recordsList);
        }else{
            return new Result(true,"暂时没有服务记录");
        }
    }
    /*
     * 获取所有的服务记录
     *
     * */
    @GetMapping("/getAllDetails")
    public Result getAllDetails(){
        List<ServiceRecordsDetails> recordsList = recordsDetailsService.getAllDetails();
        if(recordsList!=null){
            return new Result(true,recordsList);
        }else{
            return new Result(true,"暂时没有服务记录");
        }
    }
    /*
    * 在添加诊断结果时根据servicerecord_id修改服务记录的状态为4，为”已诊断“
    * */
    @GetMapping("/updateServiceRecord/{record_id}")
    public Result updateServiceRecord(@PathVariable("record_id") Integer record_id){
        int result = recordsService.updateServiceRecordById(record_id);
        if(result>0){
            return new Result(true,"修改成功");
        }else{
            return new Result(false,"修改失败");
        }
    }
    /**
     * @description:
     * @author:
     * @date: 2024/3/8 下午 09:04
     * @param:
     * @return:
     **/
    @GetMapping("/getServiceRecordsCounts")
    public Result getServiceRecordsCounts(){
        int result = recordsService.getServiceRecordsCounts();
        return new Result(true,result);

    }
    /**
     * @description:根据用户id获取所有服务记录
     * @author:
     * @date: 2024/3/30 上午 06:59
     * @param:
     * @return:
     **/
    @GetMapping("/getAllDetailsByUserId/{user_id}")
    public Result getAllDetailsByUserId(@PathVariable("user_id")Integer user_id){
        List<ServiceRecordsDetails> result = recordsDetailsService.getAllDetailsByUserId(user_id);
        if(result!=null){
            return new Result(true,result);
        }else{
            return new Result(true,"no data");
        }

    }

}
